package com.manuelvs.bobbysadventure;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.net.Uri;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class Main extends BaseGameActivity
{
	private Camera mCamera;
	
    private BitmapTextureAtlas mBitmapTextureAtlas;
    
    private TextureRegion Fondo;
    
    private TiledTextureRegion textureJugar;
    private TiledTextureRegion textureComoj;
    private TiledTextureRegion texturepracticar;
    
    AnimatedSprite onej;
    AnimatedSprite Comoj;
    AnimatedSprite twoj;
    
    private Scene mMainScene;

	@Override
	public Engine onLoadEngine() 
	{
		mCamera = new Camera(0, 0, 480, 320);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,new RatioResolutionPolicy(480, 320), mCamera));
	}

	@Override
	public void onLoadResources() {
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(1024, 512, TextureOptions.BILINEAR);
		if(getText(R.string.language).toString().equals("es"))
		{
			this.textureJugar = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "es/1j.png", 481, 0, 1, 2);
			this.textureComoj = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "es/comoj.png", 0, 321, 1, 2);
			this.texturepracticar = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "es/2j.png", 481, 151, 1, 2);
		}
		if(getText(R.string.language).toString().equals("pt"))
		{
			this.textureJugar = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "pt/1j.png", 481, 0, 1, 2);
			this.textureComoj = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "pt/comoj.png", 0, 321, 1, 2);
			this.texturepracticar = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "pt/2j.png", 481, 151, 1, 2);
		}
		if(getText(R.string.language).toString().equals("other"))
		{
			this.textureJugar = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "1j.png", 481, 0, 1, 2);
			this.textureComoj = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "howto.png", 0, 321, 1, 2);
			this.texturepracticar = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "2j.png", 481, 151, 1, 2);
		}
        this.Fondo = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "fondo.png", 0, 0);
        this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas);
	}

	@Override
	public Scene onLoadScene() {
		
	    mMainScene = new Scene();
	    
		SpriteBackground bg = new SpriteBackground(new Sprite(0, 0, Fondo));
		mMainScene.setBackground(bg);
		
		onej = new AnimatedSprite(160, 37, textureJugar)
		{
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
            {
            	if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN)
            	{
	            	onej.animate(new long[] {30}, new int[] {1}, 30);
	            	onej.setScale(1.25f);
	            	
            	}
            	if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_UP)
            	{
            		final Intent practicar = new Intent(Main.this, historyor1player.class);
	            	onej.animate(new long[] {30}, new int[] {0}, 30);
	            	onej.setScale(1f);
	            	startActivity(practicar);
            	}
            	return true;
            }
        };
	    mMainScene.attachChild(onej);
	    
	    Comoj = new AnimatedSprite(160, 227, textureComoj)
		{
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent2, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
            {
            	if(pSceneTouchEvent2.getAction()==TouchEvent.ACTION_DOWN)
            	{
            		Comoj.animate(new long[] {30}, new int[] {1}, 30);
            		Comoj.setScale(1.25f);
	            	
            	}
            	if(pSceneTouchEvent2.getAction()==TouchEvent.ACTION_UP)
            	{
            		final Intent tutorial1 = new Intent(Main.this, tutorial1.class);
            		Comoj.animate(new long[] {30}, new int[] {0}, 30);
            		Comoj.setScale(1f);
            		startActivity(tutorial1);
            	}
            	return true;
            }
        };
	    mMainScene.attachChild(Comoj);
	    
	    twoj = new AnimatedSprite(160, 132, texturepracticar)
		{
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent2, final float pTouchAreaLocalX, final float pTouchAreaLocalY)
            {
            	if(pSceneTouchEvent2.getAction()==TouchEvent.ACTION_DOWN)
            	{
            		twoj.animate(new long[] {30}, new int[] {1}, 30);
            		twoj.setScale(1.25f);
            	}
            	if(pSceneTouchEvent2.getAction()==TouchEvent.ACTION_UP)
            	{
            		twoj.animate(new long[] {30}, new int[] {0}, 30);
            		twoj.setScale(1f);
            		final Intent serverorclient = new Intent(Main.this, serverorclient.class);
            		startActivity(serverorclient);
            	}
            	return true;
            }
        };
	    mMainScene.attachChild(twoj);
	    
	    
	    mMainScene.registerTouchArea(onej);
	    mMainScene.registerTouchArea(Comoj);
	    mMainScene.registerTouchArea(twoj);
	    mMainScene.setTouchAreaBindingEnabled(true);
	    return mMainScene;
	}

	@Override
	public void onLoadComplete() {}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.more_apps:
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=ManuelVS"));
        	startActivity(browserIntent);
            return true;
        case R.id.configuration:
        	final Intent configuration = new Intent(Main.this, configuration.class);
        	startActivity(configuration);
            return true;
        }
        return false;
        
    }
}
